package org.example.dao.imp;

import org.example.dao.inter.ClassDao;
import org.example.entitys.managers.Class;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/14
 */

@Repository
public class ClassDaoImp implements ClassDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Class aClass) {
        String sql = "insert into t_manager_class(`name`,`created_time`,`class_desc`," +
                " `activity_id`) values (?,?,?,?)";
        return jdbcTemplate.update(sql,
                aClass.getName(),
                aClass.getCreated_date(),
                aClass.getClass_desc(),
                aClass.getActivity_id()
        );
    }

    @Override
    public int update(Class aClass) {
        String sql = "update t_manager_class set name = ?, class_desc = ? where id = ?";
        return jdbcTemplate.update(sql,
                aClass.getName(),
                aClass.getClass_desc(),
                aClass.getId()
        );
    }

    @Override
    public int updateTeacher(int class_id, int teacher_id) {
        String sql = "update t_manager_class set teacher_id = ? where id = ?";
        return jdbcTemplate.update(sql,
                teacher_id,
                class_id
        );
    }

    @Override
    public int updateAssistant(int class_id, int assistant_id) {
        String sql = "update t_manager_class set assistant_id = ? where id = ?";
        return jdbcTemplate.update(sql,
                assistant_id,
                class_id
        );
    }

    @Override
    public int updateActivity(Class aClass) {
        String sql = "update t_manager_class set activity_id = ? where id = ?";
        return jdbcTemplate.update(sql,
                aClass.getActivity_id(),
                aClass.getId()
        );
    }

    @Override
    public List<Class> queryByTeacher(int teacher_id) {
        String sql = "select id, name, created_time, class_desc, teacher_id, assistant_id, " +
                "activity_id from t_manager_class where teacher_id = ? ";
        return jdbcTemplate.query(sql, this::transform, teacher_id);
    }

    @Override
    public List<Class> queryByActivity(int activity_id) {
        String sql = "select id, name, created_time, class_desc, teacher_id, assistant_id " +
                "activity_id from t_manager_class where activity_id = ? ";
        return jdbcTemplate.query(sql, this::transform, activity_id);
    }

    @Override
    public int addStudentInClass(int class_id, int student_id) {
        String sql = "insert into t_teaching_student_in_class(`class_id`," +
                " `student_id`) values (?, ?)";
        return jdbcTemplate.update(sql, class_id, student_id);
    }

    @Override
    public int delete(int id) {
        String sql = "delete from t_manager_class where id = ? ";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public boolean isExitById(int id) {
        String sql = "select count(*) from t_manager_class where id = ? ";
        return jdbcTemplate.queryForObject(sql, Integer.class, id) > 0;
    }

    @Override
    public Class queryById(int id) {
        String sql = "select id, name, created_time, class_desc, teacher_id, assistant_id " +
                "activity_id from t_manager_class where id = ? ";
        return jdbcTemplate.queryForObject(sql, this::transform, id);
    }

    @Override
    public int queryLastInsertID() {
        String sql = "select LAST_INSERT_ID()";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public List<Class> queryAll() {
        String sql = "select id, name, created_time, class_desc, teacher_id, assistant_id, " +
                "activity_id from t_manager_class ";
        return jdbcTemplate.query(sql, this::transform);
    }

    @Override
    public Class transform(ResultSet rs, int RowNum) throws SQLException {
        return new Class(
                rs.getInt("id"),
                rs.getString("name"),
                rs.getDate("created_time"),
                rs.getString("class_desc"),
                rs.getInt("teacher_id"),
                rs.getInt("assistant_id"),
                rs.getInt("activity_id")
        );
    }

}
